package com.xoa.dev.pz.controller;


import com.xoa.model.users.Users;

import com.xoa.dev.pz.model.PzHouseRent;
import com.xoa.dev.pz.service.PzHouseRentService;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/sv1/pzhouserent")
public class PzHouseRentController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    @Autowired
    private PzHouseRentService pzHouseRentService;

    @RequestMapping("/houseRentManager")
    public String houseRentManager() {
        return "app/pz/houseRent/houseRentManager";
    }

    @RequestMapping("/houseInfo")
    public String houseInfo() {
        return "app/pz/houseRent/houseInfo";
    }

    @RequestMapping("/houseRentInfo")
    public String houseRentInfo() {
        return "app/pz/houseRent/houseRentInfo";
    }

    @RequestMapping("/houseServiceInfo")
    public String houseServiceInfo() {
        return "app/pz/houseRent/houseServiceInfo";
    }

    /**
     * 增加
     *
     * @param pzHouseRent
     * @return
     */
    @ResponseBody
    @RequestMapping("/insertrent")
    public ToJson insetRent(PzHouseRent pzHouseRent, HttpServletRequest request) {
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        pzHouseRent.setCreateUser(user.getUserId());
        pzHouseRent.setCreateTime(DateFormat.getDatestr(new Date()));
        return pzHouseRentService.insetRent(pzHouseRent);
    }

    /**
     * 删除
     *
     * @param rentId
     * @return
     */

    @ResponseBody
    @RequestMapping("/deleterent")
    public ToJson deleteRent(int rentId) {
        return pzHouseRentService.deleteRent(rentId);
    }

    /**
     * 修改
     *
     * @param pzHouseRent
     * @return
     */
    @ResponseBody
    @RequestMapping("/updatarent")
    public ToJson updataRent(PzHouseRent pzHouseRent) {
        return pzHouseRentService.updataRent(pzHouseRent);
    }

    /**
     * 主键查询
     *
     * @param rentId
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectrentbyrentid")
    public ToJson selectRentByrentId(int rentId) {
        return pzHouseRentService.selectRentByrentId(rentId);
    }

    /**
     * 关注度降序查询
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectrentorderbyrentconcern")
    public ToJson selectRentOrderByrentConcern(Integer page, Integer pageSize, boolean useFlag, PzHouseRent pzHouseRent, @RequestParam(value = "output", required = false, defaultValue = "0") int output, HttpServletRequest request, HttpServletResponse response) {
        return pzHouseRentService.selectRentOrderByrentConcern(page, pageSize, useFlag, pzHouseRent, output, request, response);
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年12月11日 下午16:02:00
     * 方法介绍:   出租房导入
     * 参数说明:
     * 返回值说明:
     */
    @ResponseBody
    @RequestMapping("/inputPzHouseRent")
    public ToJson<PzHouseRent> inputPzHouseRent(HttpServletRequest request, HttpServletResponse response, MultipartFile file, HttpSession session) {
        return pzHouseRentService.inputPzHouseRent(request, response, file, session);
    }
}
